package xman.mjava.lang;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

public class ClientPool {
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private GenericObjectPool clientPool = new GenericObjectPool(new ClientPoolObjectFactory());
    
    @SuppressWarnings("rawtypes")
    class ClientPoolObjectFactory implements PoolableObjectFactory {
        
        @Override
        public void activateObject(Object arg0) throws Exception {
            
        }
        
        @Override
        public void destroyObject(Object arg0) throws Exception {
            
        }
        
        @Override
        public Object makeObject() throws Exception {
            return null;
        }
        
        @Override
        public void passivateObject(Object arg0) throws Exception {
            
        }
        
        @Override
        public boolean validateObject(Object arg0) {
            return false;
        }
    }
    
    public Object get() {
        try {
            return clientPool.borrowObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public boolean release(Object obj) {
        try {
            clientPool.returnObject(obj);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
